package com.fzj.example.socket.tcp.nonblock.bio;

import java.io.*;

/**
 * ClassName: SendDataHandler
 * date: 2019/11/18 11:23
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class SendDataHandler implements  Runnable {

    private DataOutputStream dataOutputStream;
    private String type;

    SendDataHandler(String type,OutputStream out){
        this.type=type;
        this.dataOutputStream=new DataOutputStream(out);
    }

    @Override
    public void run() {
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String line="";
            while ((line=br.readLine())!=null){
                System.out.println(this.type+":"+line);
                this.dataOutputStream.writeUTF(this.type+":"+line);
                this.dataOutputStream.flush();
            }
        }catch (Exception e){
            try {
                this.dataOutputStream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
